##########################################################################################

library(dplyr)
library(data.table)
library(optparse)
library(reshape)

##########################################################################################

option_list <- list(
    make_option(c("--sig_igc_file"), type = "character") ,
    make_option(c("--sig_dgc_file"), type = "character") ,
    make_option(c("--sig_im_file"), type = "character") ,
    make_option(c("--reprot_file"), type = "character") ,
    make_option(c("--mutRate_file"), type = "character") ,
    make_option(c("--out_path"), type = "character")
)

if(1!=1){
    
    sig_igc_file <- "~/20220915_gastric_multiple/dna_combinePublic/mutsig/IGC/sig_genes.txt"
    sig_dgc_file <- "~/20220915_gastric_multiple/dna_combinePublic/mutsig/DGC/sig_genes.txt"
    sig_im_file <- "~/20220915_gastric_multiple/dna_combinePublic/mutsig/IM/sig_genes.txt"
    mutRate_file <- "~/20220915_gastric_multiple/dna_combinePublic/images/mutRate/MutRate.tsv"
    reprot_file <- "~/20220915_gastric_multiple/dna_combinePublic/public_ref/SMG_sort.list"
    out_path <- "~/20220915_gastric_multiple/dna_combinePublic/mutsig_check"

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

sig_igc_file <- opt$sig_igc_file
sig_dgc_file <- opt$sig_dgc_file
sig_im_file <- opt$sig_im_file
reprot_file <- opt$reprot_file
mutRate_file <- opt$mutRate_file
out_path <- opt$out_path

dir.create(out_path , recursive = T)

###########################################################################################

dat_sig_igc <- data.frame(fread(sig_igc_file))
dat_sig_dgc <- data.frame(fread(sig_dgc_file))
dat_sig_im <- data.frame(fread(sig_im_file))
dat_mutRate <- data.frame(fread(mutRate_file))
dat_report <- data.frame(fread(reprot_file , header = F))

###########################################################################################
q_t <- 0.20

dat_sig_igc <- data.frame( gene = dat_sig_igc$gene , q_igc = dat_sig_igc$q )
dat_sig_dgc <- data.frame( gene = dat_sig_dgc$gene , q_dgc = dat_sig_dgc$q )
dat_sig_im <- data.frame( gene = dat_sig_im$gene , q_im = dat_sig_im$q )

dat_sig <- merge( dat_sig_igc , dat_sig_dgc , all = T)
dat_sig <- merge( dat_sig , dat_sig_im , all = T )
dat_sig <- subset( dat_sig , q_im <= q_t | q_igc <= q_t | q_dgc <= q_t )

###########################################################################################
## 突变率
dat_mutRate_tmp <- subset( dat_mutRate , Hugo_Symbol %in% dat_sig$gene )
dat_mutRate_tmp <- dat_mutRate_tmp[,c("Hugo_Symbol" , "Class" , "From" , "MutRate")]
dat_mutRate_tmp <- cast(dat_mutRate_tmp , Hugo_Symbol~Class+From)

dat_mutRate_tmp$report <- ifelse( dat_mutRate_tmp$Hugo_Symbol %in% dat_report$V1 , TRUE , FALSE )
dat_final <- merge( dat_sig , dat_mutRate_tmp , by.x = "gene" , by.y  = "Hugo_Symbol" )

out_name <- paste0( out_path , "/driver.summary.tsv" )
write.table( dat_final , out_name , row.names = F , sep = '\t' , quote = F  )
